package com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CTrucks;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.Gps18DeviceList;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.Gps18DeviceListMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.IGps18DeviceListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.Gps18DeviceListVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.TrucksVo;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lvNuo
 * @since 2025-04-01
 */
@Service
public class Gps18DeviceListServiceImpl extends ServiceImpl<Gps18DeviceListMapper, Gps18DeviceList> implements IGps18DeviceListService {

    @Override
    public SzResult getList(Integer page, Integer pageSize, Integer macNumber, String fullname) {
        QueryWrapper<Gps18DeviceList> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(macNumber != null,"MAC_NUMBER",macNumber);设备编号或者id

        queryWrapper.like(StringUtils.isNotEmpty(fullname),"FULLNAME",fullname);
        if (macNumber != null) {
            queryWrapper.and(queryWrapper1 -> queryWrapper1.like("MAC_NUMBER", macNumber).or().like("MACID", macNumber));
        }
        queryWrapper.orderByAsc("BETTERRY");
        IPage<Gps18DeviceList> iPage = page(new Page<>(page, pageSize), queryWrapper);
        List<Gps18DeviceListVo> list = iPage.getRecords().stream().map(item->{
            Gps18DeviceListVo gps18DeviceListVo = new Gps18DeviceListVo();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            BeanUtil.copyProperties(item,gps18DeviceListVo);
            String betterry = item.getBetterry();
            if (betterry.indexOf('V') != -1){
                gps18DeviceListVo.setBetterry(String.valueOf(Integer.parseInt(betterry.replace("V", "")) - 100));
            }
            gps18DeviceListVo.setBlockdate(item.getBlockdate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            gps18DeviceListVo.setServerTime(sdf.format(new Date(item.getServerTime())));
            gps18DeviceListVo.setUpdtime(sdf.format(new Date(item.getUpdtime())));
            gps18DeviceListVo.setGpstime(sdf.format(new Date(item.getGpstime())));
            return gps18DeviceListVo;
        }).collect(Collectors.toList());

        Long total = iPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", list);
        return SzResult.success(map);
    }

    @Override
    public SzResult edit(Gps18DeviceList gps18DeviceList) {
        if (!checkMacNumber(gps18DeviceList)) {
            return SzResult.error("设备号检验不通过");
        }
        System.out.println(gps18DeviceList);
        return SzResult.success(updateById(gps18DeviceList));
    }

    @Override
    public SzResult add(Gps18DeviceList gps18DeviceList) {
        if (!checkMacNumber(gps18DeviceList)) {
            return SzResult.error("设备号检验不通过");
        }
        return SzResult.success(save(gps18DeviceList));
    }

    @Override
    public SzResult del(String id) {
        if (removeById(id)) {
           return SzResult.error("删除成功");
        }
        return SzResult.error("删除失败");
    }

    private Boolean checkMacNumber(Gps18DeviceList gps18Device){
        Integer macNumber = gps18Device.getMacNumber();
        String id = gps18Device.getObjectid();
        QueryWrapper<Gps18DeviceList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("MAC_NUMBER",macNumber);
        Gps18DeviceList gps18DeviceList = getOne(queryWrapper);
        if (macNumber == 0){
            return false;
        }
        if (gps18DeviceList == null){
            return true;
        }
        if (StringUtils.isEmpty(id)){
           return false;
        }
        if (id.equals(gps18DeviceList.getObjectid())){
            return true;
        }
        return false;
    }
}
